#include "Globals.h"
#include "itexture.h"


//----------------------------------------------------------------
//----------------------------------------------------------------
//	d3d format properties
//----------------------------------------------------------------
static struct
{
	D3DFORMAT	fmt;
	u32			bitsPerPixel;
	bool		compressed;
	bool		pow2only;

} formatProperties[] =
{
	{	D3DFMT_A8R8G8B8,	32,		false,	false		},
	{	D3DFMT_X8R8G8B8,	32,		false,	false		},
	{	D3DFMT_A8B8G8R8,	32,		false,	false		},
	{	D3DFMT_X8B8G8R8,	32,		false,	false		},

	{	D3DFMT_R8G8B8,		24,		false,	false		},

	{	D3DFMT_A4R4G4B4,	16,		false,	false		},
	{	D3DFMT_X4R4G4B4,	16,		false,	false		},
	{	D3DFMT_A1R5G5B5,	16,		false,	false		},
	{	D3DFMT_X1R5G5B5,	16,		false,	false		},
	{	D3DFMT_R5G6B5,		16,		false,	false		},
	{	D3DFMT_R3G3B2,		16,		false,	false		},
	{	D3DFMT_A8L8,		16,		false,	false		},

	{	D3DFMT_A8,			8,		false,	false		},
	{	D3DFMT_L8,			8,		false,	false		},
	{	D3DFMT_A4L4,		8,		false,	false		},

	{	D3DFMT_DXT1,		4,		true,	true		},
	{	D3DFMT_DXT2,		8,		true,	true		},
	{	D3DFMT_DXT3,		8,		true,	true		},
	{	D3DFMT_DXT4,		8,		true,	true		},
	{	D3DFMT_DXT5,		8,		true,	true		},
};


static u32 GetBitsPerPixel( D3DFORMAT fmt )
{
	for( u32 i=0;i<countof(formatProperties);i++ )
	{
		if( formatProperties[i].fmt==fmt )
			return formatProperties[i].bitsPerPixel;
	}
	return 32;
}


//--------------------------------------------------------------------------------
//	initialize a texture asset
//--------------------------------------------------------------------------------
void *ITexture::AssetInit( IAssetFile *pi, const CMasterIndexEntry *pme, void *context )
{
	CGraphics	*pGraphics = (CGraphics*)context;
	ITexture	*ret;
	void		*pchunk;
	u32			length;

	ret = (ITexture*)pi->GetChunk(pme->nameHash,pme->primaryChunk);
	if( ret )
	{
		// get the texture chunk
		// create the texture
		pchunk = pi->GetChunk(pme->nameHash,*(LOCID*)&ret->pTexture,&length);
		ret->pTexture = pGraphics->CreateTexture(ret->size[0],ret->size[1],ret->format,ret->numLevels,pchunk);
	}
	return (void*)ret;
}

//--------------------------------------------------------------------------------
//	uninitialize a texture asset
//--------------------------------------------------------------------------------
void ITexture::AssetUninit( void *ptr, void *context )
{
	ITexture	*pTex = (ITexture *)ptr;

	if( pTex->pTexture )
	{
		pTex->pTexture->Release();
		pTex->pTexture = 0;
	}
}


//--------------------------------------------------------------------------------
//	fixup an asset
//--------------------------------------------------------------------------------
void ITexture::AssetFixup( CAssetFileMgr *pMgr, const IAssetFile *pFile, void *ptr, const CMasterIndexEntry *pEntry )
{
}
